package com.example.mysqltry.api;


import com.example.mysqltry.api.param.MallUserLoginParam;
import com.example.mysqltry.api.param.MallUserUpdateParam;
import com.example.mysqltry.api.vo.MallUserVO;
import com.example.mysqltry.commom.Constants;
import com.example.mysqltry.config.annotation.TokenToMallUser;
import com.example.mysqltry.dao.MallUserMapper;
import com.example.mysqltry.entity.MallUser;
import com.example.mysqltry.service.BeeMallUserService;
import com.example.mysqltry.service.impl.BeeMallUserServiceImpl;
import com.example.mysqltry.util.BeanUtil;
import com.example.mysqltry.util.Result;
import com.example.mysqltry.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Constraint;
import javax.validation.Valid;

@RestController
@Api(value = "v1", tags = "新蜂商城用户操作相关接口")
@RequestMapping("/api/v1")
public class NewBeeMallPersonalAPI {
    @Resource
    private  BeeMallUserService beeMallUserService;
    @PostMapping("/user/login")
    @ApiOperation(value = "登录接口", notes = "返回token")
    public Result<String> login(@RequestBody @Valid MallUserLoginParam mallUserLoginParam)
    {
       String loginResult=beeMallUserService.login(mallUserLoginParam.getLoginName(),mallUserLoginParam.getPasswordMd5());
       if(StringUtils.hasLength(loginResult)&&loginResult.length()== Constants.TOKEN_LENGTH)
       {
           Result result = ResultGenerator.genSuccessResult();
           result.setData(loginResult);
           return result;

       }
        return ResultGenerator.genFailResult(loginResult);


    }


    @GetMapping(value = "/test1")
    @ApiOperation(value = "测试接口", notes = "方法中含有@TokenToMallUser注解")
    @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")


    public Result<String> test1(@TokenToMallUser MallUser user) {
        //此接口含有@TokenToMallUser注解，即需要登陆验证的接口。
        Result result = null;
        if (user == null) {
            //如果通过请求header中的token未查询到用户的话即token无效，登陆验证失败，返回未登录错误码。
            result = ResultGenerator.genErrorResult(416, "未登录！");
            return result;
        } else {
            //登陆验证通过。
            result = ResultGenerator.genSuccessResult("登陆验证通过");
        }
        return result;
    }

    @PostMapping("/user/info")
    @ApiOperation(value = "获取用户信息", notes = "返回token")
    @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")

    public Result<String> getUserDetails(@TokenToMallUser MallUser mallUser) {
        MallUserVO mallUserVO=new MallUserVO();
        BeanUtil.copyProperties( mallUser,mallUserVO);
        return ResultGenerator.genSuccessResult(mallUserVO);
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "更新客户", notes = "返回token")
    @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")

    public Result<String> updateUser(@TokenToMallUser MallUser mallUser, @RequestBody MallUserUpdateParam mallUserUpdateParam) {

        Boolean flag= beeMallUserService.updateUser(mallUserUpdateParam, mallUser.getUserId());

        if (flag) {
            //返回成功
            Result result = ResultGenerator.genSuccessResult();
            return result;
        } else {
            //返回失败
            Result result = ResultGenerator.genFailResult("修改失败");
            return result;
        }


    }

    @PostMapping("/user/logout")
    @ApiOperation(value = "客户登出", notes = "返回token")
    @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")

    public Result<String> logoutUser(@TokenToMallUser MallUser mallUser) {
        Boolean flag= beeMallUserService.logoutUser(mallUser);
        if (flag) {
            //返回成功
            Result result = ResultGenerator.genSuccessResult();
            return result;
        } else {
            //返回失败
            Result result = ResultGenerator.genFailResult("修改失败");
            return result;
        }
    }





    }
